package cn.edu.ctbu.dp01.service;

import cn.edu.ctbu.dp01.dao.TeacherRepository;
import cn.edu.ctbu.dp01.entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherService {
    @Autowired
    public TeacherRepository teacherRepository;

    /**
     * 读所有的教师列表
     * @return List Student
    */
    public List<Teacher> getAll(){
        return teacherRepository.findAll();
    }

     /**
      * 按id进行查询
      * @param id 主键，整数
      * @return
      */
    public Teacher findById(Integer id){
        return teacherRepository.findById(id).get();
    }

    /**
     * 按名字进行查询
     * @param name
     * @return
     */
    public List<Teacher> findByName(String name){
        return teacherRepository.findByName(name);
    }

    /**
     * 按名字和密码查询
     * @param name
     * @param password
     * @return
     */
    public List<Teacher> findByNameAndPassword(String name, String password){
        return teacherRepository.findByNameAndPassword(name, password);
    }

    /**
     * 插入
     * @param teacher
     */
    public void insert(Teacher teacher){
        teacherRepository.save(teacher);
    }

    /**
     * 更新教师
     * @param teacher
     * @return
     */
    public Teacher update(Teacher teacher){
        return teacherRepository.save(teacher);
    }

    /**
     * 删除教师
     * @param id
     */
    public void delete(Integer id){
        teacherRepository.deleteById(id);
    }

    /**
     * 新增教师
     * @param teacher
     * @return
     */
    public Teacher add(Teacher teacher){
        return teacherRepository.save(teacher);
    }
}
